package by.kniazhych.core.view.frames {
	import by.kniazhych.core.data.game.GameData;
	import by.kniazhych.core.data.game.PeaceData;
	import by.kniazhych.core.data.game.PeacePosition;
	import by.kniazhych.core.events.ApplicationEvent;
	import by.kniazhych.core.events.GameEvent;
	import by.kniazhych.core.frames.ApplicationFrame;
	import by.kniazhych.core.view.game.GameContainer;
	import by.kniazhych.core.view.game.peace.PeaceView;
	import by.kniazhych.core.view.game.selector.Selector;
	import by.vaukalak.core.events.DataBaseEvent;
	import com.greensock.TweenLite;
	import design.board.BlackCell;
	import design.board.TronCell;
	import design.board.WhiteCell;
	import design.frames.GameFrameGraphics;
	import flash.display.DisplayObject;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
	
	
	public class GameFrameView extends BaseFrameView{
		
		private var _gameLevel:GameContainer;
		private var _uiLevel:Sprite;
		private const _peacesLinker:Dictionary = new Dictionary(true);
		
		private var _gameData:GameData;
		private var _selector:Selector;
		
		static private const LEFT_OFFSET:Number = 60;
		static private const TOP_OFFSET:Number = 60;
		static private const CELL_SIDE_SIZE:Number = 50;
		
		public function GameFrameView() {
			super();
			_init();
		}
		
		private function _init():void{
			_gameLevel = new GameContainer();
			_selector = new Selector();
			addChild(_gameLevel);
			for (var i:int = 0; i < 9; i++) {
				for (var j:int = 0; j < 9; j++) {
					var cell:DisplayObject;
					if (i == 4 && j == 4) cell = new TronCell();
					else if (i % 2 == j % 2) cell = new WhiteCell();
					else cell = new BlackCell();
					_gameLevel.addChild(cell);
					cell.x = LEFT_OFFSET + CELL_SIDE_SIZE * (j + 0.5);
					cell.y = TOP_OFFSET + CELL_SIDE_SIZE * (i + 0.5);
				}
			}
			//узровень гуі, павінен быць вышэй за ігравы ўзровень
			_uiLevel = new Sprite();
			addChild(_uiLevel);
			var assets:GameFrameGraphics = new GameFrameGraphics();
			_uiLevel.addChild(assets);
			assets.exitButton.addEventListener(MouseEvent.CLICK, _handleExitButtonClick);
		}
		
		//--------------------------------------------------------------------------
		//
		//  PUBLIC METHODS
		//
		//--------------------------------------------------------------------------
		
		public function observe(gameData:GameData):void {
			_gameData = gameData;
			_gameData.addEventListener(GameEvent.PEACE_MOVED, _onPeaceMoved);
			_gameLevel.color = _gameData.color;
		}
		
		public function addPeaceView(view:PeaceView):void {
			
			_gameLevel.addChild(view);
			_peacesLinker[view.data] = view;
			view.data.addEventListener(DataBaseEvent.REMOVED_FROM_DATABASE, _onPeaceRemoved);
			view.render(false);
			//_renderPeace(view);
		}
		
		public function calculatePeaceIndexes(peace:PeaceView):PeacePosition {
			var x:uint = Math.max(Math.min((peace.x - LEFT_OFFSET) / CELL_SIDE_SIZE, 8), 0);
			var y:uint = Math.max(Math.min((peace.y - TOP_OFFSET) / CELL_SIDE_SIZE, 8), 0);
			return new PeacePosition(x, y);
		}
		
		public function showSelector(items:Array):void {
			if (stage) {
				_selector.populate(items);
				this.uiLevel.addChild(_selector);
				_selector.x = stage.stageWidth / 2 - _selector.width / 2;
				_selector.y = stage.stageHeight / 2 - _selector.height / 2;
				this._gameLevel.mouseChildren = false;
			}			
		}
		
		public function hideSelector():void {
			if (this.uiLevel.contains(_selector)) {
				this.uiLevel.removeChild(_selector);
			}
			this._gameLevel.mouseChildren = true;
		}
		
		//--------------------------------------------------------------------------
		//
		//  PRIVATE METHODS
		//
		//--------------------------------------------------------------------------
		
		private function _renderPeace(peace:PeaceView):void {
			peace.x = LEFT_OFFSET + (peace.data.position.x + 0.5) * CELL_SIDE_SIZE;
			peace.y = TOP_OFFSET + (peace.data.position.y + 0.5) * CELL_SIDE_SIZE;
		}
		
		//--------------------------------------------------------------------------
		//
		//  EVENT HANDLERS
		//
		//--------------------------------------------------------------------------
		
		private function _onPeaceRemoved(e:DataBaseEvent):void {
			_gameLevel.removeChild(PeaceView(_peacesLinker[e.currentTarget]));
			delete _peacesLinker[e.currentTarget];
		}
		
		private function _onPeaceMoved(e:GameEvent):void {
			var view:PeaceView = _peacesLinker[e.target] as PeaceView;
			
		}
		
		private function _handleExitButtonClick(e:MouseEvent):void {
			dispatchEvent(new ApplicationEvent(ApplicationEvent.FRAME_CHANGED, ApplicationFrame.MENU));
		}
		
		//--------------------------------------------------------------------------
		//
		//  PROPERTIE ACSESSORS
		//
		//--------------------------------------------------------------------------
		
		public function get gameLevel():Sprite {
			return _gameLevel;
		}
		
		public function get uiLevel():Sprite {
			return _uiLevel;
		}
		
	}

}